package cn.bird.storm.config;

import cn.bird.storm.component.jackson.StormJackson2ObjectMapperBuilderCustomizer;
import cn.bird.storm.component.exception.GlobalExceptionHandler;
import cn.bird.storm.component.log.WebLogAspect;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
@EnableConfigurationProperties(JacksonProperties.class)
@EnableAspectJAutoProxy
@Import({WebLogAspect.class})
public class StormDefaultConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(ApplicationContext applicationContext, JacksonProperties jacksonProperties) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
        new StormJackson2ObjectMapperBuilderCustomizer(applicationContext, jacksonProperties).customize(builder);
        builder.findModulesViaServiceLoader(true);
        return builder;
    }

    @Bean
    public ObjectMapper objectMapper(ApplicationContext applicationContext, JacksonProperties jacksonProperties) {
        return jackson2ObjectMapperBuilder(applicationContext, jacksonProperties).build();
    }

    @Bean
    public GlobalExceptionHandler globalExceptionHandler(){
        return new GlobalExceptionHandler();
    }

}
